package com.ithuameng.admin.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithuameng.admin.app.system.dao.DictDataMapper;
import com.ithuameng.admin.app.system.dao.DictTypeMapper;
import com.ithuameng.admin.app.system.model.DictData;
import com.ithuameng.admin.app.system.model.DictType;
import com.ithuameng.admin.app.system.model.admin.qo.DictTypeQo;
import com.ithuameng.admin.app.system.service.IDictTypeService;
import com.ithuameng.admin.utils.DictUtils;
import com.ithuameng.admin.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

    @Resource
    private DictDataMapper dictDataMapper;

    @PostConstruct
    public void init() {
        loadingDictCache();
    }

    @Override
    public IPage<DictType> listDictType(Page<DictType> page, DictTypeQo dictTypeQo) {
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        if (dictTypeQo != null) {
            if (StringUtils.isNotBlank(dictTypeQo.getDictName())) {
                queryWrapper.like(DictType.DICT_NAME, dictTypeQo.getDictName());
            }
            if (StringUtils.isNotBlank(dictTypeQo.getDictType())) {
                queryWrapper.like(DictType.DICT_TYPE, dictTypeQo.getDictType());
            }
            if (dictTypeQo.getStatus() != null) {
                queryWrapper.eq(DictType.STATUS, dictTypeQo.getStatus());
            }
        }
        return super.page(page, queryWrapper);
    }

    @Override
    public List<DictData> listDictDataByType(String dictType) {
        List<DictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    @Override
    public boolean checkDictTypeUnique(DictType dictType) {
        Long id = dictType.getId() == null ? -1L : dictType.getId();
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DictType.DICT_TYPE, dictType.getDictType());
        List<DictType> dictTypeList = super.list(queryWrapper);
        if (dictTypeList.size() == 0) {
            return true;
        }
        DictType dt = dictTypeList.get(0);
        if (dt.getId().longValue() != id.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

    @Override
    public void loadingDictCache() {
        List<DictType> dictTypeList = super.list();
        for (DictType dictType : dictTypeList) {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }

    @Override
    public boolean checkDictTypeValueExist(String dictValue, String dictType) {
        if (StringUtils.isBlank(dictType) || StringUtils.isBlank(dictValue)) {
            return false;
        }
        // 获取字典类型下的所有数据
        List<DictData> list = listDictDataByType(dictType);
        // 过滤数据
        List<DictData> result = list.stream().filter(item -> item.getDictValue().equals(dictValue)).collect(Collectors.toList());
        return result != null && result.size() > 0;
    }
}
